﻿@using Blazorise.DataGrid.Extensions;
@namespace Blazorise.Docs.Docs.Examples

<DataGrid TItem="Employee"
          Data="@employeeList"
          ReadData="@OnReadData"
          TotalItems="@totalEmployees"
          PageSize="10"
          ShowPager
          Responsive>
    <DataGridCommandColumn />
    <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" />
    <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" Editable />
    <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" Editable />
    <DataGridColumn Field="@nameof(Employee.Salary)" Caption="Salary" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" Editable>
        <EditTemplate>
            <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" ValueChanged="@( v => context.CellValue = v)" />
        </EditTemplate>
    </DataGridColumn>
</DataGrid>

<Row>
    <Column>
        <Card>
            <CardHeader>
                ODataQuery
            </CardHeader>
            <CardBody>
                <Code>@oDataQuery</Code>
            </CardBody>
        </Card>
    </Column>
</Row>

@code {
    [Inject]
    public EmployeeData EmployeeData { get; set; }
    private List<Employee> employeeList;
    private string oDataQuery;
    protected override async Task OnInitializedAsync()
    {
        employeeList = await EmployeeData.GetDataAsync();
        await base.OnInitializedAsync();
    }

    private int totalEmployees;

    private async Task OnReadData( DataGridReadDataEventArgs<Employee> e )
    {
        oDataQuery = e.ToODataString( "https://services.odata.org/V4/Northwind/Northwind.svc/Employees" );

        if ( !e.CancellationToken.IsCancellationRequested )
        {
            List<Employee> response = null;

            // this can be call to anything, in this case we're calling a fictional api
            //var response = await Http.GetJsonAsync<Employee[]>( $"some-api/employees?page={e.Page}&pageSize={e.PageSize}" );
            if ( e.ReadDataMode is DataGridReadDataMode.Virtualize )
                response = ( await EmployeeData.GetDataAsync() ).Skip( e.VirtualizeOffset ).Take( e.VirtualizeCount ).ToList();
            else if ( e.ReadDataMode is DataGridReadDataMode.Paging )
                response = ( await EmployeeData.GetDataAsync() ).Skip( ( e.Page - 1 ) * e.PageSize ).Take( e.PageSize ).ToList();
            else
                throw new Exception( "Unhandled ReadDataMode" );

            if ( !e.CancellationToken.IsCancellationRequested )
            {
                totalEmployees = ( await EmployeeData.GetDataAsync() ).Count;
                employeeList = new List<Employee>( response ); // an actual data for the current page
            }
        }
    }
}